/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package guiexample;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

class TankController implements KeyListener, MouseListener, MouseMotionListener {

    private Tank tank = null;
    private TankView view = null;
    static int mouseX = 0;
    static int mouseY = 0;
    int centreX = 0;
    int centreY = 0;
    double angle = 0;

    public void keyTyped(KeyEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void keyPressed(KeyEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_DOWN) {
            tank.setVelocity(Velocity.BACKWARD);
        } else if (key == KeyEvent.VK_UP) {
            tank.setVelocity(Velocity.FORWARD);
        } else if (key == KeyEvent.VK_LEFT) {
            tank.setRotation(Rotation.LEFT);
        } else if (key == KeyEvent.VK_RIGHT) {
            tank.setRotation(Rotation.RIGHT);
        }
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_UP) {
            if (tank.getVelocity() == Velocity.FORWARD) {
                tank.setVelocity(Velocity.NONE);
            }
        }
        if (key == KeyEvent.VK_DOWN) {
            if (tank.getVelocity() == Velocity.BACKWARD) {
                tank.setVelocity(Velocity.NONE);
            }
        }
        if (key == KeyEvent.VK_LEFT) {
            if (tank.getRotation() == Rotation.LEFT) {
                tank.setRotation(Rotation.NONE);
            }
        }
        if (key == KeyEvent.VK_RIGHT) {
            if (tank.getRotation() == Rotation.RIGHT) {
                tank.setRotation(Rotation.NONE);
            }
        }

    }

    public void mouseClicked(MouseEvent e) {
        int mouseKey = e.getButton();
        if (mouseKey == MouseEvent.BUTTON1){
            doFireCannon();
        }
        

        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mousePressed(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseReleased(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * @return the tank
     */
    public Tank getTank() {
        return tank;
    }

    /**
     * @param tank the tank to set
     */
    public void setTank(Tank tank) {
        this.tank = tank;
    }

    /**
     * @return the view
     */
    public TankView getView() {
        return view;
    }

    /**
     * @param view the view to set
     */
    public void setView(TankView view) {
        this.view = view;
    }

    public void mouseDragged(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseMoved(MouseEvent e) {
        mouseX = e.getX();
        mouseY = e.getY();
    }

    void doFireCannon(){
    }

    void doUpdateCalculations(){
        tank.updateState();
        double adj = 0;
        double opp = 0;
        centreX = (int)tank.getX() + (int)(tank.getWidth()/2);
        centreY = (int)tank.getY() + (int)(tank.getHeight()/2);
        adj = mouseX - centreX;
        opp = mouseY - centreY;
        angle = (Math.atan2(opp,adj)); //Determining angle
        tank.tx = centreX + (tank.ra*Math.cos(angle));
        tank.ty = centreY + (tank.ra*Math.sin(angle));
    }
}
